package net.joint_painting.client.view;

import gwt.g2d.client.graphics.KnownColor;
import gwt.g2d.client.graphics.Surface;
import net.joint_painting.client.observer.ViewObject;
import net.joint_painting.client.observer.ViewObserverListener;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author CAM
 * @version $Revision: 1.0 $
 */
public class ColorPicker extends Composite {

    /**
     * @author CAM
     * @version $Revision: 1.0 $
     */
    interface ColorPickerUiBinder extends UiBinder<Widget, ColorPicker> {
    }

    private static ColorPickerUiBinder uiBinder = GWT
	    .create(ColorPickerUiBinder.class);
    @UiField
    Image black;
    @UiField
    Image white;
    @UiField
    Image red;
    @UiField
    Image green;
    @UiField
    Image blue;

    @UiField
    Image yellow;

    private ViewObserverListener viewObserverListener;

    public ColorPicker() {
	initWidget(uiBinder.createAndBindUi(this));
	initColorPicker();
    }

    /**
     * Method addViewObserver.
     * 
     * @param viewObserverListener
     *            ViewObserverListener
     */
    public void addViewObserver(ViewObserverListener viewObserverListener) {
	this.viewObserverListener = viewObserverListener;
    }

    private void initColorPicker() {
	black.setUrl(new Surface().fillBackground(KnownColor.BLACK).toDataURL());
	white.setUrl(new Surface().fillBackground(KnownColor.WHITE).toDataURL());
	red.setUrl(new Surface().fillBackground(KnownColor.RED).toDataURL());
	green.setUrl(new Surface().fillBackground(KnownColor.GREEN).toDataURL());
	yellow.setUrl(new Surface().fillBackground(KnownColor.YELLOW)
		.toDataURL());
	blue.setUrl(new Surface().fillBackground(KnownColor.BLUE).toDataURL());
    }

    /**
     * Method onBlackClick.
     * 
     * @param event
     *            ClickEvent
     */
    @UiHandler("black")
    void onBlackClick(ClickEvent event) {
	viewObserverListener.notify(new ViewObject(this,
		ViewObject.ViewObjectType.SETPAINTERCOLOR, KnownColor.BLACK));
    }

    /**
     * Method onBlueClick.
     * 
     * @param event
     *            ClickEvent
     */
    @UiHandler("blue")
    void onBlueClick(ClickEvent event) {
	viewObserverListener.notify(new ViewObject(this,
		ViewObject.ViewObjectType.SETPAINTERCOLOR, KnownColor.BLUE));
    }

    /**
     * Method onGreenClick.
     * 
     * @param event
     *            ClickEvent
     */
    @UiHandler("green")
    void onGreenClick(ClickEvent event) {
	viewObserverListener.notify(new ViewObject(this,
		ViewObject.ViewObjectType.SETPAINTERCOLOR, KnownColor.GREEN));
    }

    /**
     * Method onRedClick.
     * 
     * @param event
     *            ClickEvent
     */
    @UiHandler("red")
    void onRedClick(ClickEvent event) {
	viewObserverListener.notify(new ViewObject(this,
		ViewObject.ViewObjectType.SETPAINTERCOLOR, KnownColor.RED));
    }

    /**
     * Method onWhiteClick.
     * 
     * @param event
     *            ClickEvent
     */
    @UiHandler("white")
    void onWhiteClick(ClickEvent event) {
	viewObserverListener.notify(new ViewObject(this,
		ViewObject.ViewObjectType.SETPAINTERCOLOR, KnownColor.WHITE));
    }

    /**
     * Method onYellowClick.
     * 
     * @param event
     *            ClickEvent
     */
    @UiHandler("yellow")
    void onYellowClick(ClickEvent event) {
	viewObserverListener.notify(new ViewObject(this,
		ViewObject.ViewObjectType.SETPAINTERCOLOR, KnownColor.YELLOW));
    }
}
